<template>
  <div class="flex login-container">
    <div class="login_middle">
      <div class="right_link1">
        <img src="../../assets/images/logo_title.png" alt="">
			</div>
    </div>
    <div class="formClass">
        <el-form v-if="showType === 'forget'" ref="form" :model="form" :rules="rules" class="forget_form"
          auto-complete="off">
          <div class="title-container">
            <h3 class="title" style="font-size: 32px;">忘记密码</h3>
          </div>
          <el-form-item>
            <span class="svg-container">
              <svg-icon :size="16" icon-class="mobile" />
            </span>
            <el-input v-model="form.mobile" maxlength="11" placeholder="请输入手机号" @input="handleMobileChange" />
          </el-form-item>
          <el-form-item style="border:none" prop="code">
            <div class="flex">
              <div class="flex-1 text-left border_box">
                <span class="svg-container" style="padding-left: 8px;">
                  <svg-icon :size="16" icon-class="msg" />
                </span>
                <el-input v-model="form.code" style="width:130px" placeholder="请输入验证码" autocomplete="off" />
              </div>
              <div class="inline-block pointer text-center code_btn" type="primary" @click="getPhoneCode()">
                {{ showTime ? timeCount + 's后重新获取' : '获取验证码' }}
              </div>
            </div>
          </el-form-item>
          <el-form-item prop="pwd">
            <span class="svg-container">
              <svg-icon :size="16" icon-class="password" />
            </span>
            <el-input v-model="form.pwd" placeholder="请输入新登陆密码" show-password autocomplete="new-password" />
          </el-form-item>
          <el-form-item prop="confirmPwd">
            <span class="svg-container">
              <svg-icon :size="16" icon-class="password" />
            </span>
            <el-input v-model="form.confirmPwd" placeholder="请确认新登陆密码" show-password autocomplete="new-password" />
          </el-form-item>
          <el-button style="width:100%;background-color: #E43C3A;color: #fff;" @click.native.prevent="submit">确认
          </el-button>
          <p class="text-center" style="color:#999999;font-size:16px;line-height:35px">
            <span class="pointer" @click="changeType">返回登录</span>
          </p>
        </el-form>
        <el-form v-else ref="loginForm" :model="loginForm" :rules="loginRules" class="login_form" auto-complete="on"
          label-position="left">
          <div class="title-container">
            <div class="logoTitle"></div>
            <h1 class="title">挺想租</h1>
          </div>
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon :size="16" icon-class="username" />
            </span>
            <el-input v-model="loginForm.username" ref="username" maxlength="11" placeholder="手机号" name="username"
              type="text" tabindex="1" auto-complete="on" @input="handleNameChange" />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon :size="16" icon-class="password" />
            </span>
            <el-input v-model="loginForm.password" ref="password" placeholder="密码" name="password" tabindex="2"
              show-password autocomplete="on" @keyup.enter.native="handleLogin" />
          </el-form-item>
          <p class="flex flex-between-center" style="margin-bottom:20px">
            <span class="pointer" @click="changeType" style="color:#F2A2A1;">忘记密码</span>
          </p>
          <el-button :loading="loading" style="width:100%;margin-bottom:30px;background-color: #E43C3A;color: #fff;"
            @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
      </div>
  </div>
</template>

<script>
import { checkMobile } from '@/utils/validate'
import { forgetPwd, sendMobileCode } from '@/api/login'
export default {
  name: 'Login',
  data() {
    var validateConfirm = (rule, value, callback) => {
      if (value !== this.form.pwd) {
        callback(new Error('两次输入密码不同！'))
      } else {
        callback()
      }
    }
    var validatePwd = (rule, value, callback) => {
      let reg = /^[^\u4e00-\u9fa5]{0,}$/;
      if (value.length < 6) {
        callback(new Error('密码必须大于等于6个字符'))
      } else if (!reg.test(value)) {
        callback(new Error('密码不能包含汉字！'))
      } else {
        callback()
      }
    }
    const validateUsername = (rule, value, callback) => {
      if (!checkMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePwd }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      hasVerify: false,
      showVerify: false,
      showType: 'login',
      form: {
        mobile: '',
        code: '',
        pwd: '',
        confirmPwd: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateUsername, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePwd, trigger: 'blur' }
        ],
        confirmPwd: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validateConfirm, trigger: 'blur' }
        ],
      },
      showTime: false, // 显示60s倒计时
      timeCount: '', // 计时显示
      timer: null // 计时
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    changeType() {
      this.showType = this.showType === 'login' ? 'forget' : 'login'
      this.$nextTick(() => {
        if (this.showType === 'login') {
          this.showVerify = false
          this.loginForm = {
            username: '',
            password: ''
          }
          this.showTime = false
          clearInterval(this.timer)
          this.timer = null
          this.$refs.loginForm.clearValidate()
        } else {
          this.form = {
            mobile: '',
            code: '',
            pwd: '',
            confirmPwd: ''
          }
          this.$refs.form.clearValidate()
        }
      })
    },
    submit() {
      let that = this
      this.$refs['form'].validate((valid) => {
        if (valid) {
          forgetPwd({
            mobile: this.form.mobile,
            pwd: this.form.pwd ? this.$md5(this.form.pwd) : '',
            code: this.form.code
          }).then(res => {
            this.$alert('<span><i class="el-icon-circle-check success_icon"></i></span><br />修改密码成功！去登录。', '', {
              confirmButtonText: '知道了',
              dangerouslyUseHTMLString: true,
              center: true,
              showClose: false,
              closeOnClickModal: false,
              closeOnPressEscape: false,
              customClass: 'success_msgbox',
              callback: action => {
                that.changeType()
              }
            })
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      });
    },
    getCodes(mobile) {
      sendMobileCode(mobile).then((res) => {
        this.showTime = true
        const TIME_COUNT = 60
        this.timeCount = 60
        this.timer = setInterval(() => {
          if (this.timeCount > 0 && this.timeCount <= TIME_COUNT) {
            this.timeCount--
          } else {
            this.showTime = false
            clearInterval(this.timer)
            this.timer = null
          }
        }, 1000)
      })
    },
    getPhoneCode() {
      if (this.showTime === true) {
        return;
      }
      var reg = /^1[3-9]\d{9}$/
      if (!this.form.mobile) {
        this.$message.warning('手机号不能为空')
      } else if (!reg.test(this.form.mobile)) {
        this.$message.error('手机号格式不正确')
      } else {
        this.getCodes(this.form.mobile)
      }
    },
    handleNameChange() {
      this.loginForm.username = this.loginForm.username.replace(/[^\d]/g, '')
    },
    handleMobileChange() {
      this.form.mobile = this.form.mobile.replace(/[^\d]/g, '')
    },
    onSuccess() {
      this.hasVerify = true;
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      //去掉图片验证
      // if (!this.hasVerify) {
      //   this.showVerify = true;
      //   return;
      // }
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', {
            username: this.loginForm.username,
            password: this.$md5(this.loginForm.password)
          }).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch((error) => {
            this.loading = false
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
@supports (-webkit-mask: none) and (not (cater-color: #ffffff)) {
  .login-container .el-input input {
    color: #ffffff;
  }
}

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #ffffff;
  overflow: hidden;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      // caret-color: #ffffff;
    }
  }

  .el-form-item {
    border: 1px solid #dddddd;
    background: #ffffff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style lang="scss" scoped>
.border_box {
  padding: 0 12px;
  border: 1px solid #eeeeee;
  border-radius: 3px;

  /deep/.el-input__inner {
    border: none;
  }

  svg {
    vertical-align: middle;
  }
}

.code_btn {
  width: 130px;
  margin-left: 20px;
  line-height: 45px;
  color: $mainBlue;
  border: 1px solid $mainBlue;
  border-radius: 3px;
}

.login-container {
  /deep/.el-form-item__content {
    line-height: 30px;
  }

  .login_top {
    height: 75px;
    width: 1200px;
    margin: 0 auto;
    position: relative;

    .left_logo {
      float: left;
      height: 48px;
      width: 172px;
      margin-top: 15px;
      background: url(../../assets/images/login_logo.png) no-repeat;
      background-size: 100%;
    }



    .right_link2 {
      cursor: pointer;
      font-size: 16px;
      float: right;
      line-height: 75px;

      :hover {
        color: #53a6ff;
      }
    }
  }

  .login_middle {
    // display: flex;
    // justify-content: flex-end;
    align-items: center;
    width: 75%;
    min-height: 100vh;
    background: url(../../assets/images/login_bg1.png) center center no-repeat;
    background-size: 100% 100%;
    text-align: center;
  }

  .logoBg {
    // width: 75%;
    // height: auto;
   
    // background-size: cover;
    // text-align: center;
    // min-height: 100%;
    // height: 100%;
  }

  .right_link1 {
    cursor: pointer;
    position: absolute;
    left: 80px;
    top: 80px;
    color: #fff;

    // :hover {
    //   color: #E43C3A;
    // }
  }

  .login_footer {
    // width: 1200px;
    // margin: 0 auto;
    text-align: center;
    // margin-top: 620px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #999999;
    text-decoration: none;
    margin: 20px 0;

    a:hover {
      color: #53a6ff;
    }
  }

  .formClass {
    width: 35%;
    display: flex;
    justify-content: center;  
    align-items: center;      
  }

  .login_form,
  .forget_form {
    width: 350px;
    margin-right: 150px;
    max-width: 100%;
    padding: 30px 30px 15px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #ffffff;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    // position: relative;
    // top: -10px;
    // left: 600px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #889aa4;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 60px;
      color: #333;
      margin: 0px auto 40px auto;
      text-align: center;
    }

    .logoTitle {
      width: 177px;
      height: 185px;
      background: url(../../assets/images/logo-1.png) no-repeat center;
      position: relative;
      left: 80px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
}
</style>
